// Ninf-G-XX, ver. 0.1
// Copyright 2011 National Institute of Advanced Industrial Science and Technology
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __NC_SESSION_MANAGER_H__
#define __NC_SESSION_MANAGER_H__


#include <map>
using namespace std;
#include "Waiting.hpp"
#include "Session.hpp"
#include "LogParser.hpp"

namespace ninf { 
namespace client {

struct ltint
{
  bool operator()(int i1, int i2)
  {
    return i1 < i2;
  }
};


class SessionManager {
  map<int, Session *> sessions;

  // parser to parse condor log file
  LogParser         parser;
  // a map from condor id to session id
  map<int, int, ltint> idMap;

  // a map from session id to condor id
  map<int, int, ltint> revMap;

  void handle(LogEntry & entry);
  int lastID;

public:
  SessionManager(int pid);

  void cancel(int id);
  void cancelAll(void);

  bool probe(int id); // return true if it's done, false otherwise
  bool probeOr(int * idList, int length , int * idP);

  int wait(int id);
  
  int waitAll();
  
  int waitAny();

  int waitAnd(int * idList, int length);

  int waitOr(int * idList, int length);

  int wait(Waiting & waiting);

  int createUnreportedIDList(int ** pIdList);
  int getSubmittedJobId();
  void throttling();

  void registerSession(Session * session);
  void registerMap(int sessionID, int condorID);
  Session * getSession(int id);
	void getFailedSessionID(int * idP);
  
  void perror(const char * str, FILE * fp);
};
}
}
#endif
